package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.common.model.UserExample;
import com.suncaper.tddmall.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/my_account")
public class UserAccountController {
    @Autowired
    UserAccountService userAccountService;

   @RequestMapping("")
   public String displayMyAccount(HttpSession httpSession){
       if (httpSession.getAttribute("user")!=null){
           return "../static/page/my_account";
       }
       return "register";
   }



    @RequestMapping("/account_details")
    public String displayUserInfo(HttpSession httpSession, Model model){
        User user = (User)httpSession.getAttribute("user");
        User info = userAccountService.searchUserInfo(user.getUserId());
        model.addAttribute("userInfo",info);

        System.out.println(info.getSex()+","+info.getPassword()+","+info.getEmail());

        return "../static/page/account_details";
    }

    @PostMapping("/info_update")
    public String updateUserInfo(HttpSession httpSession, User user){
        User olduser = (User)httpSession.getAttribute("user");
        user.setUserId(olduser.getUserId());
        userAccountService.updateUserInfoByUserId(user);

        return "redirect:/my_account";
    }

    @RequestMapping("/logout")
    public String logoutInAccount(HttpSession httpSession){
       httpSession.removeAttribute("user");
       return "../static/page/create_account";
    }

}
